package activity.live;

import com.hichip.campro.R;
import com.hichip.content.HiChipDefines;
import com.hichip.sdk.HiChipP2P;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import bean.MyCamera;
import utils.HiToast;

/**
 * 直播界面的model
 *
 * @author lt
 */
public class LiveViewModel implements TextWatcher, OnClickListener {
    private static LiveViewModel instance = null;
    private static final Object LOCK = new Object();
    private TextView tvNum;
    private String roomInput;
    private Context mContext;
    private MyCamera mMyCamera;
    public static int select_preset;
    //public int mFlagPreset=0;   //1-设置  2-调用
    public LiveViewActivity mAct;
    private static String mUid;

    private LiveViewModel() {
    }

    public static LiveViewModel getInstance() {
        //这是最标准的懒汉式单例模式  线程安全  并在线程安全效率低的情况下稍微提高了效率ssss
        if (instance == null) {
            synchronized (LOCK) {
                instance = new LiveViewModel();
            }
        }
        return instance;
    }

    /**
     * 预置位弹出的数字键盘
     *
     * @param numKeyBoard
     */
    public void handKeyBoard(Context context, View numKeyBoard, MyCamera myCamera) {
        this.mContext = context;
        if (context instanceof LiveViewActivity) {
            mAct = (LiveViewActivity) context;
        }
        this.mMyCamera = myCamera;
        tvNum = (TextView) numKeyBoard.findViewById(R.id.tv_key_preset);
        tvNum.addTextChangedListener(this);
        tvNum.setOnClickListener(this);
        if(select_preset!=0&&mUid==myCamera.getUid()){
            tvNum.setText(select_preset+"");
        }else {
            select_preset=0;
        }
        mUid=myCamera.getUid();

        TextView tv_one = numKeyBoard.findViewById(R.id.tv_one);
        TextView tv_two = numKeyBoard.findViewById(R.id.tv_two);
        TextView tv_there = numKeyBoard.findViewById(R.id.tv_there);
        TextView tv_four = numKeyBoard.findViewById(R.id.tv_four);
        TextView tv_five = numKeyBoard.findViewById(R.id.tv_five);
        TextView tv_six = numKeyBoard.findViewById(R.id.tv_six);
        TextView tv_seven = numKeyBoard.findViewById(R.id.tv_seven);
        TextView tv_eight = numKeyBoard.findViewById(R.id.tv_eight);
        TextView tv_nine = numKeyBoard.findViewById(R.id.tv_nine);
        TextView tv_zero = numKeyBoard.findViewById(R.id.tv_zero);
        TextView tv_delete = numKeyBoard.findViewById(R.id.tv_delete);

        TextView tv_preset_set = numKeyBoard.findViewById(R.id.tv_preset_set);
        TextView tv_preset_call = numKeyBoard.findViewById(R.id.tv_preset_call);

        tv_preset_set.setOnClickListener(this);
        tv_preset_call.setOnClickListener(this);

        tv_one.setOnClickListener(this);
        tv_two.setOnClickListener(this);
        tv_there.setOnClickListener(this);
        tv_four.setOnClickListener(this);
        tv_five.setOnClickListener(this);
        tv_six.setOnClickListener(this);
        tv_seven.setOnClickListener(this);
        tv_eight.setOnClickListener(this);
        tv_nine.setOnClickListener(this);
        tv_zero.setOnClickListener(this);
        tv_delete.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_one:
                roomInput = tvNum.getText().toString().trim();
                tvNum.setText(roomInput + "1");
                break;
            case R.id.tv_two:
                roomInput = tvNum.getText().toString().trim();
                tvNum.setText(roomInput + "2");
                break;
            case R.id.tv_there:
                roomInput = tvNum.getText().toString().trim();
                tvNum.setText(roomInput + "3");
                break;
            case R.id.tv_four:
                roomInput = tvNum.getText().toString().trim();
                tvNum.setText(roomInput + "4");
                break;
            case R.id.tv_five:
                roomInput = tvNum.getText().toString().trim();
                tvNum.setText(roomInput + "5");
                break;
            case R.id.tv_six:
                roomInput = tvNum.getText().toString().trim();
                tvNum.setText(roomInput + "6");
                break;
            case R.id.tv_seven:
                roomInput = tvNum.getText().toString().trim();
                tvNum.setText(roomInput + "7");
                break;
            case R.id.tv_eight:
                roomInput = tvNum.getText().toString().trim();
                tvNum.setText(roomInput + "8");
                break;
            case R.id.tv_nine:
                roomInput = tvNum.getText().toString().trim();
                tvNum.setText(roomInput + "9");
                break;
            case R.id.tv_zero:
                roomInput = tvNum.getText().toString().trim();
                tvNum.setText(roomInput + "0");
                break;
            case R.id.tv_delete:
                tvNum.setText("");
                break;
            case R.id.tv_preset_call:
                int preset_call = select_preset - 1;
                if (!TextUtils.isEmpty(tvNum.getText().toString().trim())) {
                    select_preset = Integer.parseInt(tvNum.getText().toString().trim());
                    Log.i("tedu", "--LiveViewModel--HI_P2P_SET_PTZ_PRESET--:");
                    mMyCamera.sendIOCtrl(HiChipDefines.HI_P2P_SET_PTZ_PRESET, HiChipDefines.HI_P2P_S_PTZ_PRESET.parseContent(HiChipP2P.HI_P2P_SE_CMD_CHN, HiChipDefines.HI_P2P_PTZ_PRESET_ACT_CALL, preset_call));
                    if (mAct != null) {
                        mAct.mFlagPreset = 2;
                    }
                }
                break;
            case R.id.tv_preset_set:
                //Log.i("tedu","--我点击了设置：--:");
                String str = tvNum.getText().toString().trim();
                int preset_set = select_preset - 1;
                if (TextUtils.isEmpty(str) || Integer.parseInt(str) > 255 || "0".equals((str.charAt(0) + ""))) {
                    HiToast.showToast(mContext, mContext.getString(R.string.tip_perset_toast));
                } else {
                    Log.i("tedu", "--LiveViewModel--HI_P2P_SET_PTZ_PRESET--:");
                    mMyCamera.sendIOCtrl(HiChipDefines.HI_P2P_SET_PTZ_PRESET, HiChipDefines.HI_P2P_S_PTZ_PRESET.parseContent(HiChipP2P.HI_P2P_SE_CMD_CHN, HiChipDefines.HI_P2P_PTZ_PRESET_ACT_SET, preset_set));
                    if (mAct != null) {
                        mAct.mFlagPreset = 1;
                    }
                }
                break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (!TextUtils.isEmpty(s.toString().trim())) {
            Integer num = Integer.parseInt(s.toString());
            select_preset = num;
            if (num == 0 || num > 255) {
                HiToast.showToast(mContext, mContext.getString(R.string.tip_perset_toast));
            }
        }
    }

}
